﻿@using BootstrapBlazor.Shared.Samples.Charts
@using Utility = BootstrapBlazor.Shared.Samples.Charts.Utility
@inject IStringLocalizer<Bar> Localizer

<Chart ChartType="ChartType.Bar" OnInitAsync="() => OnInit(false)" OnAfterInitAsync="@OnAfterInit" OnAfterUpdateAsync="@OnAfterUpdate" @ref="BarChart" />
<div class="text-center mt-2 chart">
    <div class="btn-group">
        <button class="btn btn-info" data-method="play" @onclick="@OnPlayChart">
            <i class="fa-solid fa-circle-play"></i>
            <span>@Localizer["BarTypeAnimationOn"]</span>
        </button>
        <button class="btn btn-info" data-method="stop" @onclick="@OnStopChart">
            <i class="fa-solid fa-circle-stop"></i>
            <span>@Localizer["BarTypeAnimationOff"]</span>
        </button>
    </div>
</div>
<div class="text-center mt-2 chart">
    <div class="btn-group">
        <button class="btn btn-primary" @onclick="e => Utility.RandomData(BarChart)"><i class="fa-solid fa-chart-column"></i><span>@Localizer["BarTypeRandomData"]</span></button>
        <button class="btn btn-primary" @onclick="OnReloadChart"><i class="fa-solid fa-chart-column"></i><span>@Localizer["BarTypeReload"]</span></button>
        <button class="btn btn-primary" @onclick="e => Utility.AddDataSet(BarChart, ref _barDatasetCount)"><i class="fa-solid fa-circle-plus"></i><span>@Localizer["BarTypeAddDataSet"]</span></button>
        <button class="btn btn-primary" @onclick="e => Utility.RemoveDataSet(BarChart, ref _barDatasetCount)"><i class="fa-solid fa-circle-minus"></i><span>@Localizer["BarTypeRemoveDataSet"]</span></button>
        <button class="btn btn-primary" @onclick="e => Utility.AddData(BarChart, ref _barDataCount)"><i class="fa-solid fa-plus"></i><span>@Localizer["BarTypeAddData"]</span></button>
        <button class="btn btn-primary" @onclick="e => Utility.RemoveData(BarChart, ref _barDataCount)"><i class="fa-solid fa-minus"></i><span>@Localizer["BarTypeRemoveData"]</span></button>
    </div>
</div>
<ConsoleLogger @ref="Logger" class="mt-3" />

@code {
    private Random Randomer { get; } = new Random();

    private int _barDatasetCount = 2;

    private int _barDataCount = 7;

    [NotNull]
    private Chart? BarChart { get; set; }

    [NotNull]
    private ConsoleLogger? Logger { get; set; }

    /// <summary>
    /// OnAfterRender
    /// </summary>
    /// <param name="firstRender"></param>
    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);

        if (firstRender)
        {
            Logger.Log("Bar loading data ...");
        }
    }

    private Task OnAfterInit()
    {
        Logger.Log("Bar initialization is complete");
        return Task.CompletedTask;
    }

    private Task OnAfterUpdate(ChartAction action) => InvokeAsync(() => Logger.Log($"Bar Figure update data operation completed -- {action}"));

    private Task<ChartDataSource> OnInit(bool stacked, bool setTitle = true)
    {
        var ds = new ChartDataSource();
        if (setTitle)
        {
            ds.Options.Title = "Bar Histogram";
        }
        ds.Options.X.Title = "days";
        ds.Options.Y.Title = "Numerical value";
        ds.Options.X.Stacked = stacked;
        ds.Options.Y.Stacked = stacked;
        ds.Labels = Enumerable.Range(1, _barDataCount).Select(i => i.ToString());
        for (var index = 0; index < _barDatasetCount; index++)
        {
            ds.Data.Add(new ChartDataset()
            {
                Label = $"Set {index}",
                Data = Enumerable.Range(1, _barDataCount).Select(i => Randomer.Next(20, 37)).Cast<object>()
            });
        }
        return Task.FromResult(ds);
    }

    private CancellationTokenSource _chartCancellationTokenSource = new();

    private Task OnPlayChart()
    {
        _chartCancellationTokenSource = new CancellationTokenSource();
        return Task.Run(async () =>
        {
            while (!_chartCancellationTokenSource.IsCancellationRequested)
            {
                await Task.Delay(800, _chartCancellationTokenSource.Token);
                if (!_chartCancellationTokenSource.IsCancellationRequested) await Utility.RandomData(BarChart);
            }
        });
    }

    private void OnStopChart() => _chartCancellationTokenSource.Cancel();

    /// <summary>
    /// 强刷控件,重新初始化控件外观
    /// </summary>
    private Task OnReloadChart()
    {
        _barDataCount = Randomer.Next(5, 15);
        BarChart.Reload();
        return Task.CompletedTask;
    }
}
